package com.kk.xhr.model.service.impl;

import com.kk.xhr.common.pojo.BinaryEnum;
import com.kk.xhr.model.assembler.AdjustSalaryStructKit;
import com.kk.xhr.model.dao.AdjustSalaryDynamicSqlSupport;
import com.kk.xhr.model.dao.AdjustSalaryMapper;
import com.kk.xhr.model.dto.AdjustSalaryDTO;
import com.kk.xhr.model.entity.AdjustSalary;
import com.kk.xhr.model.service.IAdjustSalaryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;

/**
 * 调薪服务接口实现
 *
 * @author kkmystery
 * @version 1.0 2021/3/6
 * @since 1.0.0
 */
@Repository
@RequiredArgsConstructor
public class AdjustSalaryServiceImpl implements IAdjustSalaryService {
    private final AdjustSalaryMapper adjustSalaryMapper;
    private final AdjustSalaryStructKit instance = AdjustSalaryStructKit.INSTANCE;

    /**
     * {@inheritDoc}
     */
    @Override
    public Optional<AdjustSalaryDTO> queryById(Long id) {
        return adjustSalaryMapper.selectOne(dsl -> dsl
            .where(AdjustSalaryDynamicSqlSupport.id, isEqualTo(id))
            .and(AdjustSalaryDynamicSqlSupport.status, isEqualTo(BinaryEnum.TRUE.getValue())))
            .map(instance::toDto);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<AdjustSalaryDTO> queryByUserId(Long userId) {
        return adjustSalaryMapper.select(dsl -> dsl
            .where(AdjustSalaryDynamicSqlSupport.userId, isEqualTo(userId)))
            .stream()
            .map(instance::toDto)
            .collect(Collectors.toList());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean add(AdjustSalaryDTO dto) {
        return 1 == adjustSalaryMapper.insertSelective(instance.toEntity(dto));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean modify(AdjustSalaryDTO dto) {
        if (null == dto.getId()) {
            return false;
        }
        return 1 == adjustSalaryMapper.updateByPrimaryKeySelective(instance.toEntity(dto));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean modifyStatus(Long id, BinaryEnum status) {
        if (null == status) {
            return false;
        }
        return 1 == adjustSalaryMapper.updateByPrimaryKeySelective(new AdjustSalary().withId(id).withStatus(status.getValue()));
    }
}
